package com.seek.inter;

import com.alibaba.fastjson.JSONObject;
import com.seek.authcheck.constants.AuthConstant;
import com.seek.cache.CacheUtil;
import com.seek.common.handler.SpringContextHolder;
import com.seek.constants.ApiParamsConstant;
import com.seek.constants.RedisCustomerConstant;
import com.seek.inter.interdto.InterDtoCUser;
import com.seek.model.dto.ResultInfo;
import com.seek.model.entity.TCustomerZfb;
import com.seek.service.CustomerService;
import com.seek.service.CustomerZfbService;
import com.seek.service.ReadService;
import com.seek.util.AESPhpUtil;
import com.seek.util.CommonUtil;
import com.seek.util.EncryptUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by weh on 2017/7/5.
 */
public class CustomerZfbInter {

  private CustomerZfbService customerZfbService = (CustomerZfbService) SpringContextHolder
      .getBean("customerZfbService");

  private CustomerService customerService = (CustomerService) SpringContextHolder
      .getBean("customerService");

  private ReadService readService = (ReadService) SpringContextHolder.getBean("readService");


  /**
   * 处理提交的支付宝认证记录
   */
  @Transactional
  public ResultInfo saveZfbCertify(HttpServletRequest request, JSONObject reqJson)
      throws Exception {

    ResultInfo result = new ResultInfo();
    Long companyId = CommonUtil.getLongValue(reqJson.getString(ApiParamsConstant.COMPANY_ID));
    String token = reqJson.getString(ApiParamsConstant.TOKEN);
    Long custId = CommonUtil.getLongValue(reqJson.getString(ApiParamsConstant.CUST_ID));
    String loginName = reqJson.getString(ApiParamsConstant.LOGIN_NAME);
    String loginPwd = reqJson.getString(ApiParamsConstant.LOGIN_PWD);

    if (StringUtils.isEmpty(loginName)) {
      result.setStatus(1);
      result.setMessage("请输入支付宝登录账号");
      return result;
    }

    if (StringUtils.isEmpty(loginPwd)) {
      result.setStatus(1);
      result.setMessage("请输入支付宝登录密码");
      return result;
    }

    TCustomerZfb zfb = new TCustomerZfb();
    zfb.setCompanyId(companyId);
    zfb.setCustomerId(custId);
    zfb.setLoginName(loginName);
    zfb.setLoginPwd(loginPwd);
    zfb.setSubmitTime(new Date());
    zfb.setAuditStatus(0);
    zfb.setLsbz(0);
    customerZfbService.executeUpdate(
        "update t_customer_zfb set lsbz = ? where  company_id = ? and customer_id = ?",
        new Object[]{1, companyId, custId});
    customerZfbService.save(zfb);
    String updateSql = "update t_customer set zfb_status = ? where id =? and company_id = ? ";
    customerService.executeUpdate(updateSql, new Object[]{1, custId, companyId});

    Map<String, Object> cacheData = new HashMap<>();
    cacheData.put(RedisCustomerConstant.ZFB_STATUS, AuthConstant.AUTH_PROCESS);
    CacheUtil.refreshCustRedis(token, cacheData);
    result.setStatus(0);
    result.setMessage("成功");
    return result;
  }

  /**
   * 获取支付宝认证信息
   **/
  public ResultInfo getZfbCertify(HttpServletRequest request, JSONObject reqJson) {
    ResultInfo result = new ResultInfo();
    Long companyId = CommonUtil.getLongValue(reqJson.getString(ApiParamsConstant.COMPANY_ID));
    Long custId = CommonUtil.getLongValue(reqJson.getString(ApiParamsConstant.CUST_ID));
    String query =
        "select login_name as loginName,DATE_FORMAT(submit_time,'%Y-%m-%d %H:%i:%s') as submitTime,"
            + "DATE_FORMAT(audit_time,'%Y-%m-%d %H:%i:%s') as auditTime,audit_status as auditStatus,"
            + "audit_remark as auditRemark from t_customer_zfb where company_id = ? and customer_id= ? limit 1";

    Map<String, Object> data = readService
        .findOneMapBySql(query, new Object[]{companyId, custId});
    if (MapUtils.isNotEmpty(data)) {
      result.setStatus(0);
      result.setBody(data);
    } else {
      result.setStatus(1);
      result.setMessage("没有查询到支付宝认证信息");
    }

    return result;
  }


}
